/**
 * ...
 * @author Yog
 */

package cookinmaddy.client;
import cookinmaddy.model.Ingredient;
import haxe.Serializer;
import haxe.Unserializer;
import js.Dom;

class IngredientView 
{
	public static var DOC_ID : String = "";

	var mainDoc : Document;
	var innerDoc : Document;	
	var addDiv : Document;
	
	var list : List<Ingredient> ;
	
	
	public function new() {	}
	
	public function show()
	{
		if (mainDoc == null)
		{
			mainDoc = cast js.Lib.document.getElementById(DOC_ID);
			var req = "?action=listingredients";
			StringTools.htmlEscape(req);
			Ajax.getContentAsync(req, onAjaxReturn_LoadList);
		}
		else
		{
			load();
		}
	}	
	
	private function onAjaxReturn_LoadList(data : Dynamic)
	{
		//mainDoc.innerHTML = data;
		list = Unserializer.run(data);
		load();
	}
	
	private function load()
	{
		var ulist : HtmlDom = cast js.Lib.document.createElement("ul");
		var self = this;
		mainDoc.innerHTML = "";
		for (ingredient in list)
		{			
			var node :HtmlDom = cast js.Lib.document.createElement("li");
			
			
			var editbtn : Button = cast js.Lib.document.createElement("button");
			editbtn.innerHTML = "Edit";
			DomListener.add(editbtn, "click", function(e)
			{
				self.onClick_Edit(node, ingredient);
			});

			var delbtn : Button = cast js.Lib.document.createElement("button");
			delbtn.innerHTML = "Delete";
			DomListener.add(delbtn, "click", function(e)
			{
				self.onClick_Delete(node, ingredient);
			});
			node.innerHTML = ingredient.Name;
			node.appendChild(delbtn);
			node.appendChild(editbtn);
			//trace("nb childs = " + node.childNodes.length);
			ulist.appendChild(node);		
		}

		innerDoc = cast js.Lib.document.createElement("div");
		innerDoc.id = "innerDoc";
		innerDoc.appendChild(ulist);	
		var btn : Button  = cast js.Lib.document.createElement("button");
		btn.innerHTML = "Add";
		DomListener.add(btn, "click", function(e)
		{
			self.onClick_DoAdd();
		});
		addDiv = cast js.Lib.document.createElement("div");
		addDiv.appendChild(btn);
		innerDoc.appendChild(addDiv);		
		mainDoc.appendChild(innerDoc);	
	}
	
	private function onClick_Edit(node : HtmlDom, ingredient : Ingredient)
	{
		node.innerHTML = "";
		var name_txt : Dom.Text = cast js.Lib.document.createElement("text");
		name_txt.innerHTML = "Name: ";
		var name_input : Dom.Text = cast js.Lib.document.createElement("input");
		name_input.defaultValue = ingredient.Name;
		name_input.id = "name";
		
		node.appendChild(name_txt);
		node.appendChild(name_input);
		var self = this;
		var btn : Button = cast js.Lib.document.createElement("button");
		btn.innerHTML = "Update";
		DomListener.add(btn, "click", function(e)
		{
			self.onClick_Update(node, ingredient);
		});		
		var cancelbtn : Button = cast js.Lib.document.createElement("button");
		cancelbtn.innerHTML = "Cancel";
		DomListener.add(cancelbtn, "click", function(e)
		{
			self.show();
		});				
		node.appendChild(btn);
		node.appendChild(cancelbtn);
	}

	private function onClick_Delete(node : HtmlDom, ingredient : Ingredient)
	{
		var data = Serializer.run(ingredient);
		var req = "?action=delingredient&data=" + data;
		StringTools.htmlEscape(req);
		Ajax.getContentAsync(req, onAjaxReturn_Deleted);		
	}

	private function onAjaxReturn_Deleted(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var id : Int = cast data;
			for (unit in list)
			{
				if (unit.ID == id)
				{
					list.remove(unit);
					show();
					break;
				}
			}
			
		}
	}
	
	private function onClick_DoAdd()
	{
		addDiv.innerHTML = "";
		var name_txt : Dom.Text = cast js.Lib.document.createElement("text");
		name_txt.innerHTML = "Name: ";
		var name_input : Dom.Text = cast js.Lib.document.createElement("input");
		name_input.defaultValue = "";
		name_input.id = "name";
		
		addDiv.appendChild(name_txt);
		addDiv.appendChild(name_input);
		var self = this;
		var btn : Button = cast js.Lib.document.createElement("button");
		btn.innerHTML = "Add";
		DomListener.add(btn, "click", function(e)
		{
			self.onClick_Add(self.addDiv);
		});		
		addDiv.appendChild(btn);
		btn = cast js.Lib.document.createElement("button");
		btn.innerHTML = "Cancel";
		DomListener.add(btn, "click", function(e)
		{
			self.show();
		});		
		addDiv.appendChild(btn);
	}
	
	private function onClick_Add(div : HtmlDom)
	{
		var ingredient : Ingredient = new Ingredient();
		var l : Int = div.childNodes.length;
		for(i in 0...l)
		{
			var txt : Dom.Text = cast div.childNodes[i] ;
			switch(txt.id)
			{
				case "name":
					ingredient.Name = txt.value;
				default:
			}
		}
		
		var data = Serializer.run(ingredient);		
		var req = "?action=addingredient&data=" + data;
		StringTools.htmlEscape(req);
		Ajax.getContentAsync(req, onAjaxReturn_Added);	
	}	
	
	private function onAjaxReturn_Added(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var ingredient : Ingredient = haxe.Unserializer.run(data);
			list.add(ingredient);
			show();
		}
		else
			js.Lib.alert(ret);
	}
	
	private function onClick_Update(node : HtmlDom, ingredient : Ingredient)
	{
		var l : Int = node.childNodes.length;
		for(i in 0...l)
		{
			var txt : Dom.Text = cast node.childNodes[i] ;
			switch(txt.id)
			{
				case "name":
					ingredient.Name = txt.value;
				default:
			}
		}
		
		var data = Serializer.run(ingredient);
		//trace("(" + data.length + ") " + data);
		//trace("js: " + data.length);
		//trace("js: " + data);
		var req = "?action=updateingredient&data=" + StringTools.urlEncode(data);
		
		trace("js: " + req);
		Ajax.getContentAsync(req, onAjaxReturn_Updated);
	}	
	
	private function onAjaxReturn_Updated(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret == "OK")
			show();
		else
			js.Lib.alert(ret);
	}	
}